import { ESpecimenStatus, getSpecimenStatusLabel } from "@scmt/api-common";
import { SpecimenStatisticResult } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
import { useApi } from "@scmt/core-api";

const _statisticsData = ref<SpecimenStatisticResult>({
  notGeneratedNum: 0,
  generatedNum: 0,
  printedNum: 0,
  collectedNum: 0,
  submissionNum: 0,
  receiptNum: 0,
  rejectionNum: 0,
  verificationNum: 0,
  examineNum: 0,
  printedReportNum: 0,
  cancelledNum: 0,
});
const _loading = ref(false);

export const useStatistics = () => {
  const $api = useApi();

  const statisticMap: Array<[ESpecimenStatus, keyof SpecimenStatisticResult]> =
    [
      // 未生成
      [ESpecimenStatus.NOTGENERATED, "notGeneratedNum"],
      // 已生成
      [ESpecimenStatus.Done, "generatedNum"],
      // 已打印条码
      [ESpecimenStatus.PrintedBarCode, "printedNum"],
      // 已采集
      [ESpecimenStatus.Collection, "collectedNum"],
      // 已送检
      [ESpecimenStatus.Submission, "submissionNum"],
      // 已签收
      [ESpecimenStatus.Receipt, "receiptNum"],
      // 已拒收
      [ESpecimenStatus.Rejection, "rejectionNum"],
      // 待审核
      [ESpecimenStatus.Verification, "verificationNum"],
      // 已审核
      [ESpecimenStatus.Examine, "examineNum"],
      // 已打印报告
      [ESpecimenStatus.PrintedReport, "printedReportNum"],
      // 已作废
      [ESpecimenStatus.Cancelled, "cancelledNum"],
    ];

  const { request, loading } = useRequest({
    statistics: (param: any) =>
      $api.lis.specimen.getCountList(param).then((res) => {
        _statisticsData.value = res;

        return res;
      }),
  });

  const statistics = computed(() =>
    statisticMap.map(([state, key]) => {
      const value = _statisticsData.value[key] || 0;

      return {
        label: getSpecimenStatusLabel(state),
        value,
        state,
      };
    })
  );

  watch(
    loading,
    (val) => {
      _loading.value = val;
    },
    {
      immediate: true,
    }
  );

  const getStatistics = (param: any) => {
    request("statistics", param);
  };

  return {
    statistics,
    getStatistics,
  };
};
